#include "topconn.ch"
#INCLUDE "rwmake.ch"
#include "ap5mail.ch"

//**************************************************************************
//* Programa......: Gsfat17.PRW     Data da Criacao..: 27/07/2006
//* Objetivo......: Emitir um relatorio de conferencia do Itau Seguro
//* 			  : separando por Veiculo/Estado
//* Setor.........: Faturamento
//* Modulo........: Faturamento          Menu..: Relatorio/Relatorios Aviacao
//* Data Revisao..:                      Motivo:
//* Programador...: Wanderson Fernandes de Souza
//***************************************************************************
User Function AVI00036()
Private lEnd       := .F.
Private lAbortPrint:= .F.
Private CbTxt      := ""
Private limite     := 080
Private tamanho    := "P"
Private nomeprog   := "AVI00036"
Private nTipo      := 18
Private aReturn    := { "Zebrado", 1, "Administracao", 2, 2, 1, "", 1}
Private nLastKey   := 0
Private cPerg      := "GslITA"
Private cbtxt      := Space(10)
Private cbcont     := 00
Private CONTFL     := 01
Private m_pag      := 01
Private wnrel      := "AVI00036"
Private cString    := "SF2"
aMatriz := {}; nPos :=0
aMatriz1 := {}; nPos1 := 0
nLin         := 0
titulo       := "Seguro"
cDesc1       := "Este programa tem como objetivo imprimir relatorio "
cDesc2       := "de acordo com os parametros informados pelo usuario."
cDesc3       := ""
cPict        := ""
Cabec1       := "PLACA  |OR|VALOR NOTA    |DES|NOTA FIS|DESTINO                |DATA"
Cabec2       := ""
imprime      := .T.
aOrd 	     := {}
nValor:=0

Pergunte(cPerg, .F.)

wnRel := SetPrint(cString,NomeProg,cPerg,@titulo,cDesc1,cDesc2,cDesc3,.T.,aOrd,.T.,Tamanho,,.T.)

IF nLastKey == 27
	Return
EndIF

SetDefault(aReturn, cString)

IF nLastKey == 27
	Return
EndIF

RptStatus({|| RunReport()}, Titulo)
Return

****************************************************
Static Function RunReport()
Local cLojaCli, cDescri, oProcess, oHtml
Local cMensagem
Local cQuery := ""
Local cAlias := ""
Local nPerc  := 0
Local wCont  := 0

cQuery += " SELECT"
cQuery +=		" SF2.F2_PLACA, SF2.F2_DTASAID, SF2.F2_EST, SF2.F2_VALBRUT, SF2.F2_DOC, SF2.F2_REDESP, SF2.F2_VEND1"
cQuery += " FROM "
cQuery += 		RetSqlName("SF2") + " SF2"
cQuery += " WHERE"
cQuery += 		" SF2.F2_FILIAL = '" + xFilial("SF2") + "' AND"
cQuery += 		" SF2.D_E_L_E_T_ <> '*' AND"
cQuery += 		" SF2.F2_DTASAID BETWEEN '" + DToS(MV_PAR01) + "' AND '" + DToS(MV_PAR02) + "' AND"
cQuery += 		" SF2.F2_PLACA <> ' '"
cQuery += " ORDER BY"
cQuery += 		" SF2.F2_EST, SF2.F2_PLACA"

cQuery := ChangeQuery(cQuery)
TCQuery cQuery NEW ALIAS (cAlias := GetNextAlias())

dbSelectArea(cAlias)

Count To wCount

SetRegua(wCount)
(cAlias)->(dbGoTop())

IF wCount > 0
	nLin := Cabec(Titulo, Cabec1, Cabec2, NomeProg, Tamanho, nTipo)
EndIF

wTotalGeral := 0

While (cAlias)->(!EOF())
	wTotCli := 0
	nLin++

	IF nLin <> 10
		@nLin, 000 PSay Replicate("_", 132)
		nLIn++
	EndIF
	
	wEstado := (cAlias)->F2_EST
	wPlaca := (cAlias)->F2_PLACA

	While (cAlias)->(!EOF()) .AND. (cAlias)->F2_EST == wEstado .AND. (cAlias)->F2_PLACA == wPlaca
		@ nLin, 000 PSay "V E I C U L O=>   " + RTrim((cAlias)->F2_PLACA)
		Linha()
		wPlaca := (cAlias)->F2_PLACA
		wEstado := (cAlias)->F2_EST

//		While (cAlias)->(!EOF()) .AND. (cAlias)->F2_EST == wEstado .AND. (cAlias)->F2_PLACA == wPlaca
			@ nLin, 000 PSay AllTrim((cAlias)->F2_PLACA) + "|" + "MG" + "|" + Transform((cAlias)->F2_VALBRUT, "99,999,999.99") + "|"

			IF (cAlias)->F2_REDESP = " "
				cUf := (cAlias)->F2_EST
			Else
				cUf := Posicione("SA4", 1, xFilial("SA4") + (cAlias)->(F2_REDESP), "A4_EST")
			EndIF
			
				@ nLin, 027 PSay cUf	+"|"
				@ nLin, 033 PSay Subs(AllTrim((cAlias)->F2_DOC), 4, 9) + "|"

				Do Case
					Case cUf = "SP"
						@ nLIn, 043 PSay "Sao Paulo     - 0,075%"
					Case cUf = "SE"
						@ nLIn, 043 PSay "Sergipe       - 0,25%"
					Case cUf = "BA"
						@ nLIn, 043 PSay "Bahia         - 0,23%"
					Case cUf = "PE"
						@ nLIn, 043 PSay "Pernanbuco    - 0,35%"
					Case cUf = "MS"
						@ nLIn, 043 PSay "Mato G.do Sul - 0,23%"
					Case cUf = "PR"
						@ nLIn, 043 PSay "Parana        - 0,18%"
					Case cUf = "SC"
						@ nLIn, 043 PSay "Santa Catarina- 0,20%"
					Case cUf = "MG"
						@ nLIn, 043 PSay "Minas Gerais  - 0,05%"
					Case cUf = "MT"
						@ nLIn, 043 PSay "Mato Grosso   - 0,25%"
					Case cUf = "GO"
						@ nLIn, 043 PSay "Goias         - 0,14%"
					Case cUf = "RJ"
						@ nLIn, 043 PSay "Rio de Janeiro- 0,13%"
					Case cUf = "ES"
						@ nLIn, 043 PSay "Espirito Santo- 0,13%"
					Case cUf = "AL"
						@ nLIn, 043 PSay "Alagoas       - 0,28%"
					Case cUf = "PA"
						@ nLIn, 043 PSay "Belem         - 0,45%"
					Case cUf = "AC"
						@ nLIn, 043 PSay "Rio Branco    - 0,55%"
					Case cUf = "RS"
						@ nLIn, 043 PSay "Porto Alegre  - 0,25%"
					Case cUf = "AL"
						@ nLIn, 043 PSay "Alagoas       - 0,28%"
				EndCase
				
			//MATRIZ
			nPos := aScan(aMatriz, {|x|x[1] == (cAlias)->F2_EST})

			IF nPos > 0
				aMatriz[nPos, 2] += (cAlias)->F2_VALBRUT
			Else
				aAdd(aMatriz, {(cAlias)->F2_EST, (cAlias)->F2_VALBRUT})
			EndIF
			
			
			nPos1 := aScan(aMatriz1, {|x|x[1] == (cAlias)->F2_PLACA})
			
			IF nPos1 > 0
				aMatriz1[nPos1, 2] += (cAlias)->F2_VALBRUT
			Else
				aAdd(aMatriz1, {(cAlias)->F2_PLACA, (cAlias)->F2_VALBRUT})
			EndIF

			@ nLIn, 066 Psay "|"
			@ nLin, 070 Psay STOD((cAlias)->F2_DTASAID)

			wTotalGeral += (cAlias)->F2_VALBRUT
			Linha()
			
			(cAlias)->(dbSkip())
			Incregua()
		EndDo
	EndDo

	IF wTotCli > 0
		@ nLin, 000 PSay "Total do Veiculo=> " + Transform(wTotCli, "@E 999,999,999.99")
		
		IF (cAlias)->F2_REDESP = " "
			cUf := (cAlias)->F2_EST
		Else
			cUf := Posicione("SA4", 1, xFilial("SA4") + (cAlias)->(F2_REDESP), "A4_EST")
		EndIF

		Do Case
			Case cUf = "AC"
				nPerc := wTotCli * 0.55 / 100
			Case cUf = "AL"
				nPerc := wTotCli * 0.28 / 100
			Case cUf = "AP"
				nPerc := wTotCli * 0.50 / 100
			Case cUf = "AM"
				nPerc := wTotCli * 0.60 / 100
			Case cUf = "BA"
				nPerc := wTotCli * 0.23 / 100
			Case cUf = "CE"
				nPerc := wTotCli * 0.50 / 100
			Case cUf = "DF"
				nPerc := wTotCli * 0.15 / 100
			Case cUf = "ES"
				nPerc := wTotCli * 0.13 / 100
			Case cUf = "GO"
				nPerc := wTotCli * 0.14 / 100
			Case cUf = "MA"
				nPerc := wTotCli * 0.40 / 100
			Case cUf = "MT"
				nPerc := wTotCli * 0.25 / 100
			Case cUf = "MS"
				nPerc := wTotCli * 0.23 / 100
			Case cUf = "MG"
				nPerc := wTotCli * 0.05 / 100
			Case cUf = "PA"
				nPerc := wTotCli * 0.45 / 100
			Case cUf = "PB"
				nPerc := wTotCli * 0.35 / 100
			Case cUf = "PR"
				nPerc := wTotCli * 0.18 / 100
			Case cUf = "PI"
				nPerc := wTotCli * 0.35 / 100
			Case cUf = "RJ"
				nPerc := wTotCli * 0.13 / 100
			Case cUf = "RS"
				nPerc := wTotCli * 0.25 / 100
			Case cUf = "RN"
				nPerc := wTotCli * 0.35 / 100
			Case cUf = "RO"
				nPerc := wTotCli * 0.50 / 100
			Case cUf = "RR"
				nPerc := wTotCli * 0.75 / 100
			Case cUf = "SC"
				nPerc := wTotCli * 0.20 / 100
			Case cUf = "SP"
				nPerc := wTotCli * 0.075 / 100
			Case cUf = "SE"
				nPerc := wTotCli * 0.25 / 100
			Case cUf = "TO"
				nPerc := wTotCli * 0.15 / 100
		EndCase
			
			@ nLin, 055 PSay Transform(nPerc, "@E 999,999,999.99")			
	EndIF
EndDo

If wTotalGeral > 0
	Linha()
	@ nLin,000 Psay "Total Geral do Carregamento=> "+Transform(wtotALgeral,   "@E 999,999,999.99")
EndIF

nTamMtr := Len(aMatriz)

If nTamMtr > 0
	Linha()
	@ NLIN,00 PSAY "..............RESUMO POR ESTADO.............."
	Linha()
	@ nLin,00 Psay "U.F                 |Valor Faturado   |   Valor Premio"
	Linha()
	aMatriz:= aSort(aMatriz,,,{|x,y| x[1] < y[1]})

	For nI = 1 To nTamMtr
		@ nlin,00 PSay aMatriz[nI, 1]

		Do Case
			Case aMatriz[nI, 1] = "SP"
				nPerc := 0.75
			Case aMatriz[nI, 1] = "AL"
				nPerc := 0.28
			Case aMatriz[nI, 1] = "BA"
				nPerc := 0.23
			Case aMatriz[nI, 1] = "PE"
				nPerc := 0.35
			Case aMatriz[nI, 1] = "MS"
				nPerc := 0.23
			Case aMatriz[nI, 1] = "PR"
				nPerc := 0.18
			Case aMatriz[nI, 1] = "RJ"
				nPerc := 0.13
			Case aMatriz[nI, 1] = "MG"
				nPerc := 0.05
			Case aMatriz[nI, 1] = "RS"
				nPerc := 0.25
			Case aMatriz[nI, 1] = "PA"
				nPerc := 0.45
			Case aMatriz[nI, 1] = "AC"
				nPerc := 0.55
		EndCase
		
		@ nLin, 20 PSay aMatriz[nI, 2] Picture "@E 999,999,999.99"
		@ nLin, 40 PSay aMatriz[nI, 2] * nPerc / 100 Picture "@E 999,999,999.99"
		Linha()
	Next nI
EndIF

nTamMtr1 := Len(aMatriz1)

If nTamMtr1 > 0
	Linha()
	@ NLIN,00 PSAY "..............RESUMO POR VEICULO.............."
	Linha()
	@ nLin,00 Psay "U.F                 |Valor Faturado   |   Valor Premio"
	Linha()
	aMatriz1:= aSort(aMatriz1,,,{|x,y| x[1] < y[1]})

	For nX = 1 To nTamMtr1
		@ nLin, 00 PSay aMatriz1[nX, 1]
		@ nLin, 20 PSay aMatriz1[nX, 2] Picture "@E 999,999,999.99"

		IF aMatriz1[nX, 2] > 180000.00
			@ nLin,50 PSay " Avisar o Dimas!!"
		EndIF
		
		Linha()
	Next nX
EndIF

dbCloseArea(cAlias)

Set Device To Screen

IF aReturn[5]==1
	dbCommitAll()
	
	SET PRINTER TO
	OurSpool(wnRel)
EndIF

MS_FLUSH()
U_AVI00315()
Return

*************************
Static Function CriaSx1()
Local aHelp:={}
PutSx1(cPerg,"01","Data da Saida de      ? ","","","mv_ch1","D",08,00,00,"G","","","","","mv_par01","","","","","","","","","","","","","","","","","")
PutSx1(cPerg,"02","Data da Saida Ate     ? ","","","mv_ch2","D",08,00,00,"G","","","","","mv_par02","","","","","","","","","","","","","","","","","")
PutSx1(cPerg,"03","Mensagem            ? ","","","mv_ch3","C",30,00,00,"G","","","","","mv_par03","","","","","","","","","","","","","","","","","")

Return

***********************
Static Function Linha()

IF nLin > 55
	nLin := Cabec(Titulo, Cabec1, Cabec2, NomeProg, Tamanho, nTipo)
EndIF

nLin++
Return
